import vue from '@vitejs/plugin-vue'
import * as path from 'path'
import { defineConfig, normalizePath } from 'vite'
import { VantResolver } from 'unplugin-vue-components/resolvers'
import { createHtmlPlugin } from 'vite-plugin-html'
import AutoImport from 'unplugin-auto-import/vite'
import Components from 'unplugin-vue-components/vite'
import legacy from '@vitejs/plugin-legacy'
// @ts-ignore
import packageJson from './package.json'
// @ts-ignore
import autoprefixer from 'autoprefixer'
// @ts-ignore
import pxToVw from 'postcss-px-to-viewport'

export default defineConfig(async ({ mode }) => {
  let title = `${packageJson.name}-${mode}`
  // if (mode !== 'development') {
  //   const v = await updateVersion(mode)
  //   title += v
  // }
  const isProd = mode === 'production'
  return {
    base: './',
    server: {
      host: '0.0.0.0',
      port: 8099,
      proxy: {
        '/api/v1/zerolimit': {
          target: 'http://111.26.43.115:10002/',
          ws: true,
          // 是否开启代理
          changeOrigin: true,
          // 路径重写
          rewrite: path => path.replace(/^\/api\/v1\/zerolimit/, '/zerolimit')
        },
        '/api': {
          target: 'http://localhost:10003/',
          // 是否启用websockets
          ws: true,
          // 是否开启代理
          changeOrigin: true,
          // 路径重写
          rewrite: path => path.replace(/^\/api/, '')
        }
      }
    },
    plugins: [
      vue(),
      legacy({
        targets: ['chrome < 60', 'edge < 15'],
        additionalLegacyPolyfills: ['regenerator-runtime/runtime'],
        polyfills: [
          'es.symbol',
          'es.array.filter',
          'es.promise',
          'es.promise.finally',
          'es/map',
          'es/set',
          'es.array.for-each',
          'es.object.define-properties',
          'es.object.define-property',
          'es.object.get-own-property-descriptor',
          'es.object.get-own-property-descriptors',
          'es.object.keys',
          'es.object.to-string',
          'web.dom-collections.for-each',
          'esnext.global-this',
          'esnext.string.match-all'
        ]
      }),
    
      AutoImport({
        imports: ['vue', 'vue-router'],
        resolvers: [VantResolver()]
      }),
      Components({
        resolvers: [VantResolver()]
      }),
      createHtmlPlugin({
        minify: true,
        /**
         * 在这里写entry后，你将不需要在`index.html`内添加 script 标签，原有标签需要删除
         * @default src/main.ts
         */
        entry: '/src/main.js',
        /**
         * 需要注入 index.html ejs 模版的数据
         */
        inject: {
          data: {
            // 查找.env.test文件里面的VITE_PROJECT_TITLE，请以VITE_标识开头
            title
          }
        }
      })
    ],
    resolve: {
      alias: {
        '@': path.resolve(__dirname, 'src'),
        // '@views': path.resolve(__dirname, 'src/views')
      }
    },
    css: {
      postcss: {
        plugins: [
          autoprefixer,
          pxToVw({
            unitToConvert: 'px',
            viewportWidth: 375,
            unitPrecision: 5,
            propList: ['*'],
            viewportUnit: 'vw',
            fontViewportUnit: 'vw',
            selectorBlackList: [],
            minPixelValue: 1,
            mediaQuery: false,
            replace: true,
            exclude: undefined,
            include: undefined,
            landscape: false,
            landscapeUnit: 'vw',
            landscapeWidth: 568
          }),
          {
            postcssPlugin: 'internal:charset-removal',
            AtRule: {
              charset: (atRule) => {
                if (atRule.name === 'charset') {
                  atRule.remove()
                }
              }
            }
          }
        ]
      },
      // css预处理
      preprocessorOptions: {
        scss: {
          //additionalData: '@import "@/assets/scss/globalVariable.scss";'
        }
      }
    },
    build: {
      outDir: 'dist',
      minify: 'terser',
      target: 'es2015',
      terserOptions: {
        compress: {
          drop_console: isProd,
          drop_debugger: isProd
        }
      },
    }
  }
})
